// pages/edit_lamp/edit_lamp.js
var app=getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    auto:false,
    lampid:'',
    lampmid:'',
    longitude: '',
    latitude: '',
    loadgroupmid: '',
    loadgroupid: '',
    loadname: '',
    districtid: '',
    districtmid: '',
    districtname: '',

    groupID:-1,
    lampname: '',
    lampbrand: '',
    lampno: '',
    lamppower: 0,
    controllerno: '',
    poweron_time: '00:01',
    poweroff_time: '23:59',
    maxelec: 0,
    minelec: 0,
    index: 0,
    lamp_type_array: [
      {
        id: "0",
        name: "普通灯"
      },
      {
        id: "1",
        name: "十字路口灯"
      },
      {
        id: "2",
        name: "主干道灯"
      },
      {
        id: "3",
        name: "人行道灯"
      },
      {
        id: "4",
        name: "隧道灯"
      }
    ]     

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    let header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    that.setData({
      districtid: options.districtid,
      districtmid: options.districtmid,
      loadgroupid: options.loadgroupid,
      loadgroupmid: options.loadgroupmid,
      lampid: options.lampid,
      lampmid:options.lampmid
    });

    wx.request({
      url: 'https://119.3.46.145/server/FindSingleLight?id=' + that.data.lampmid,
      method: 'POST',
      header: header,
      success: res => {
        //console.log(res, "res");
        //{"data":"[{\"LightType\":\"2\",\"MaxCurrent\":\"100.0\",\"turnOfftime\":\"05:00:00\",\"devNo\":\"868221043829715\",\"groupID\":\"1\",\"MinCurrent\":\"50.0\",\"Latitude\":\"32.3763\",\"Longitude\":\"119.387566\",\"Name\":\"无锡测试\",\"RatedPower\":\"0.0\",\"LightNo\":\"\",\"UserId\":\"922aeb81-29c8-41ba-9f82-36d9abb33265\",\"LightBrand\":\"\",\"id\":\"32\",\"turnOntime\":\"01:00:00\"}]"}   
        let result = JSON.parse(res.data.data)[0];
        
        that.setData({
          longitude: result.Longitude,
          latitude: result.Latitude,
          lampname: result.Name,
          lampbrand: result.LightBrand,
          lampno: result.LightNo,
          lamppower: result.RatedPower,
          controllerno: '',
          groupID: result.groupID,
          poweron_time: result.turnOntime,
          poweroff_time: result.turnOfftime,
          maxelec: result.MaxCurrent,
          minelec: result.MinCurrent,
          index: result.LightType,
          controllerno:result.devNo       
        });
      },
      fail: function (error) {
        //console.log(error, "FindSingleLight error");
        wx.showModal({
          title:'提示',
          content:error
        });
      }
    })
  },

  formSubmit: function (e) {
    let that = this;
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    let long_lati = e.detail.value.input_long_lati;
    let long_lati_arr = long_lati.split(",");

    if (e.detail.value.input_lampname == "") {
      wx.showModal({
        title: '提示',
        content: '单灯名称不能为空',
        showCancel: false
      });
      return;
    }

    if (e.detail.value.controllerno == "") {
      wx.showModal({
        title: '提示',
        content: '控制器编号不能为空',
        showCancel: false
      });

      return;
    }

    if (long_lati_arr[0] == "" || long_lati_arr[1] == "") {
      wx.showModal({
        title: '提示',
        content: '经度或者纬度不能为空',
        showCancel: false
      });

      return;
    }

    //e.detail.value.
    /**input_brand: ""
    input_controllerno: "868221047652659"
    input_lampname: "testlamp3"
    input_lampno: ""
    input_long_lati: "119.39823,32.37654"
    input_maxelec: ""
    input_minelec: ""
    input_power: ""
    picker_poweroff_time: "07:00"
    picker_poweron_time: "06:00"
    picker_type: "1"*/
    that.setData({
      lampname: e.detail.value.input_lampname,
      lampbrand: e.detail.value.input_brand,
      lampno: e.detail.value.input_lampno,
      lamppower: e.detail.value.input_power,
      controllerno: e.detail.value.input_controllerno,
      poweron_time: e.detail.value.picker_poweron_time,
      poweroff_time: e.detail.value.picker_poweroff_time,
      maxelec: e.detail.value.input_maxelec,
      minelec: e.detail.value.input_minelec,
      index: e.detail.value.picker_type,
      longitude: long_lati_arr[0],
      latitude: long_lati_arr[1]
    });

    let tmp_lamp_type = that.data.lamp_type_array[that.data.index].name;

    wx.request({
      // url: 'https://119.3.46.145/server/UpdateSingleLight?userid=' + app.globalData.userid + '&devNo=' + that.data.controllerno + '&Name=' + that.data.lampname + '&LightBrand=' + that.data.lampbrand + '&LightNo=' + that.data.lampno + '&id=' + that.data.loadgroupmid + '&RatedPower=' + that.data.lamppower + '&LightType=' + that.data.index + '&Longitude=' + that.data.longitude + '&Latitude=' + that.data.latitude + '&ontime=' + that.data.poweron_time + '&offtime=' + that.data.poweroff_time + '&maxCurrent=' + that.data.maxelec + '&minCurrent=' + that.data.minelec,
      url: 'https://119.3.46.145/server/UpdateSingleLight',
      method: 'Post',   
      header: header,
      data:{
        userid:app.globalData.userid,
        devNo:that.data.controllerno,
        Name:that.data.lampname,
        LightBrand:that.data.lampbrand,
        LightNo:that.data.lampno,      
        id: that.data.lampmid,
        RatedPower:that.data.lamppower,
        LightType:that.data.index,
        Longitude:that.data.longitude,
        Latitude:that.data.latitude,
        ontime:that.data.poweron_time,
        offtime:that.data.poweroff_time,
        MaxCurrent:that.data.maxelec,
        MinCurrent:that.data.minelec,
        auto:false
        // userid: '922aeb81-29c8-41ba-9f82-36d9abb33265',
        // devNo: '868221047652659',
        // Name: 'test2659',
        // LightBrand:'',
        // LightNo:'',
        //RatedPower: 0.0,
        //LightType: 0,
        //Longitude: 120.25635,
        //Latitude: 37.06833,
        // id: 52,
        //ontime: '02:00:00',
        //offtime: '20:59:00',
        //MaxCurrent: 200.0,
        //MinCurrent: 20.0,
        //auto: false
      },
      success: res => {
        //data: { error: "0", info: "添加成功" }  {error: "-1", info: "已存在，无法添加"}
        //console.log(res.data, "data");
        let result = res.data;

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: result.info,
            success: function () {
              wx.request({
                url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.loadgroupid,  //获取区域列表构造树
                header: header,
                success: function (res) {
                  let data = JSON.parse(res.data.data);

                  if (data.length == 0) {
                    return;
                  }


                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    lamp_list: data
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "dev_manager  onload loadTree");
                }
              });
            }
          });
        }

        if (result.error == "-1") {
          wx.showModal({
            title: '提示',
            content: result.info,
            showCancel: false
          })
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  bindpoweronChange:function(e){
    let that = this;
   
    that.setData({
      poweron_time: e.detail.value
    });

  },

  bindpoweroffChange:function(e){    
    let that =  this;
    that.setData({    
      poweroff_time: e.detail.value
    });
  },  

  bindPickerChange:function(e){
    let that = this;
    //console.log(e, "e");
    that.setData({
      index:e.detail.value
    });

  },

  formReset: function (e) {
    let that = this;

    let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
    let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值 
    wx.navigateBack(); //关闭当前页面，返回上一个页面    
  },

  choose_tude: function () {
    let that = this;
    wx.chooseLocation({     
      success(res_location) {
        //console.log(res_location, "res_location");
        that.setData({
          // hasLocation: true,
          // location: formatLocation(res.longitude, res.latitude),
          locationAddress: res_location.address,
          longitude: res_location.longitude,
          latitude: res_location.latitude,
          //name: res_location.name
        });
      }
    })
  },

  set_auto: function (e) {
    let that = this;
    that.setData({
      auto: e.detail.value
    });
  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})